package com.heyqing.medium.day9;

import com.heyqing.medium.TreeNode;

import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * ClassName:LevelOrder
 * Package:com.heyqing.medium.day9
 * Description:
 *
 * @Date:2024/10/11
 * @Author:Heyqing
 */
public class LevelOrder {
    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        List<List<Integer>> result = new ArrayList<>();
        if (Objects.isNull(root)) return result;
        while (!queue.isEmpty()) {
            List<Integer> temp = new ArrayList<>();
            int loop = queue.size();
            for (int i = 0; i < loop; i++) {
                TreeNode first = queue.poll();
                temp.add(first.val);
                if (Objects.nonNull(first.left)) {
                    queue.offer(first.left);
                }
                if (Objects.nonNull(first.right)) {
                    queue.offer(first.right);
                }
            }
            result.add(temp);
        }
        return result;
    }
}
